//
//  MusicViewController.m
//  A段项目前期
//
//  Created by 杨利佳 on 15/5/19.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "MusicViewController.h"
#import "MyScrollView.h"
#import "MusicModel.h"
#import "TouchImage.h"
#import "PlayerViewController.h"
#import "MusicCollectionViewCell.h"
#import "SANetWorkingTask.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "MusicSecViewController.h"
#import "MusicThiViewController.h"


@interface MusicViewController ()<MyScrollViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) NSMutableArray *scrollArray;
@property (nonatomic, retain) UICollectionView *coll;
@property (nonatomic, retain) NSMutableArray *collectArray;

@end

@implementation MusicViewController

- (void)dealloc
{
    [_collectArray release];
    [_coll release];
    [_scrollArray release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.scrollArray = [NSMutableArray array];
        self.collectArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.translucent = NO;
    self.title = @"音乐";
   
    //网络判断
    if ( [self isConnectionAvailable]) {
         [self createScroll];
    };
    [self createCollection];
    
}


#pragma mark - 
#pragma mark 上面的滚动图片
- (void)createScroll
{
    // 网络请求
    [SANetWorkingTask request:@"http://mobile.ximalaya.com/m/category_focus_image?categoryId=2&version=3.25.7.1&device=android" parmater:nil block:^(NSDictionary *result) {
        NSArray *array = [result objectForKey:@"list"];
        
        for (NSDictionary *dic in array) {
            MusicModel *music = [[MusicModel alloc]init];
            [music setValuesForKeysWithDictionary:dic];
            [_scrollArray addObject:music];
            [music release];
        }

        // 创建scroll
        MyScrollView *scroll = [[MyScrollView alloc] initWithFrame:CGRectMake(10, 20, self.view.frame.size.width - 20, self.view.frame.size.height / 3 - 20)];
        scroll.delegate = self;

        
        [scroll setScrollViewIamage:_scrollArray];
        [self.view addSubview:scroll];
        [scroll release];
        
    }];
    
}
//判断网络使用
-(BOOL) isConnectionAvailable{
    
    BOOL isExistenceNetwork = YES;
    Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
    switch ([reach currentReachabilityStatus]) {
        case NotReachable:
            isExistenceNetwork = NO;
            NSLog(@"没网");
            [self alertView:@"没有网络连接"];
            break;
        case ReachableViaWiFi:
            isExistenceNetwork = YES;
            NSLog(@"用的WIFI");
            break;
        case ReachableViaWWAN:
            isExistenceNetwork = YES;
            NSLog(@"3G");
            [self alertView:@"使用3G连接"];
            break;
    }
    
    
    return isExistenceNetwork;
}
// 警告框
- (void)alertView:(NSString *)str
{
    UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: @"确定", nil];
    [alert setDelegate:self];
    [alert show];
    [alert release];
    
}
//点击图片进入第二页
- (void)imageTouch:(TouchImage *)image
{
    if (image.model.trackId) {
        PlayerViewController *player = [[PlayerViewController alloc] init];
        player.trackId = image.model.trackId;
        [self presentViewController:player animated:YES completion:nil];
        [player release];
    } else
    {
        MusicThiViewController *thi = [[MusicThiViewController alloc] init];
        thi.ID = image.model.id;
        [self.navigationController pushViewController:thi   animated:YES];
        [thi release];
        
    }
    
    
}

#pragma mark -
#pragma mark 音乐下面的分类
- (void)createCollection
{
    // 网络请求
    [SANetWorkingTask request:@"http://mobile.ximalaya.com/m/category_tag_list?device=android&per_page=100&category=music&type=album&page=1" parmater:nil block:^(NSDictionary *result) {
        NSArray *arr = [result objectForKey:@"list"];
        [self.collectArray addObjectsFromArray:arr];
        
        [_coll reloadData];

    }];
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.sectionInset = UIEdgeInsetsMake(0 , 10, 20, 10);
    flow.itemSize = CGSizeMake(self.view.frame.size.width / 3 - 15, 100);
    self.coll = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 3 - 20, self.view.frame.size.width, self.view.frame.size.height - (self.view.frame.size.height / 3) - 100) collectionViewLayout:flow];
    self.coll.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    [flow release];
    
    _coll.dataSource = self;
    _coll.delegate = self;
    [self.view addSubview:_coll];
    [_coll release];
    [flow release];
    
    [_coll registerClass:[MusicCollectionViewCell class] forCellWithReuseIdentifier:@"a"];
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _collectArray.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MusicCollectionViewCell *cell = [collectionView  dequeueReusableCellWithReuseIdentifier:@"a" forIndexPath:indexPath];
    NSDictionary *dic = [_collectArray objectAtIndex:indexPath.item];
    [cell setBackgroundColor:[UIColor whiteColor]];
    [cell.pic sd_setImageWithURL:[dic objectForKey:@"cover_path"] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
    cell.category.text = [dic objectForKey:@"tname"];
    
    return cell;
    
}


// 点击分类进入下一页
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MusicSecViewController *sec = [[MusicSecViewController alloc] init];
    NSDictionary *dic = [_collectArray objectAtIndex:indexPath.item];
    sec.tname = [dic objectForKey:@"tname"];
    [self.navigationController pushViewController:sec animated:YES];
    [sec release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
